<?php

class Core_Action_Admin_System_Appearance_Section_Addblock extends Action_Admin
{
	function initiator()
	{
		parent::initiator();

		Agilet::setVar(array(
			'breadcrum' => array(
				array(
					'url' => 'admin/system',
					'label' => 'System'
				),
				array(
					'url' => 'admin/system/appearance',
					'label' => 'Appearance'
				),
				array(
					'url' => 'admin/system/appearance/section',
					'label' => 'Section'
				),
				array(
					'url' => 'admin/system/appearance/section/addblock',
					'label' => 'Add Block'
				),
			),
			'page-title' => 'Add Block',
		));
	}

	public function execute($request, $initial = false, $script = null)
	{

		$table = Model::getTable('core/appearance/block');

		$select = $table -> select();

		$form = Form::factory('core.form.admin.appearance.block');

		$section_id = $request -> getParam('section_id');

		if ($request -> isPost() and $form -> isValid($_POST))
		{
			try
			{
				$data = $form -> getData();

				$item = $table -> fetchNew();

				$item -> fromArray($data);

				$item -> section_id = $section_id;

				$item -> save();

				$form -> setNotice('Saved.');

				Response::redirect('admin/system/appearance/section');
			}
			catch(Exception $e)
			{
				DEBUG and $form -> setNotice($e -> getMessage());
			}
		}

		$request -> response = View::render($script = 'core/html/admin/edit', $data = array('form' => $form, ));
	}

}
